/**
 * Created by Administrator on 2015/9/17.
 */

function autoExpand(id) {
    var ifm= document.getElementById(id);
    var subWeb = document.frames ? document.frames[id].document : ifm.contentDocument;
    if(ifm != null && subWeb != null) {
        ifm.height = window.screen.availHeight-140;
        ifm.width = window.screen.availWidth-5;
    }
}
function open(url,id){
	var ifm= document.getElementById(id);
	ifm.src = url;
}
function cleanMonitorType(){
	//	
	var mt= document.getElementById('monitor-type');
	if(mt){
		while(mt.options.length>0){
			mt.options.remove(0); 
		}
	}
}
function cleanApplications(){
	//
	var mt= document.getElementById('applications');
	if(mt){
		while(mt.options.length>0){
			mt.options.remove(0);
		}
	}
}

function autoFlush(){
	setInterval(function () {
		if(isAutoFlush()){
			window.document.location.href=window.document.location.href;
		}
	}, 10000);
}
function isAutoFlush(){
	var obj= window.parent.document.getElementById('auto-flush');
	return obj.checked;
}

function initServerMonitorType(baseUrl){
	//	
	subBaseUrl = baseUrl;
	var mt= document.getElementById('monitor-type');

	if(mt){
			var cpuItem = new Option("CPU", "0"); 
			var netItem = new Option("Network", "1"); 
			var memItem = new Option("Memory", "2"); 
			var serverItem = new Option("Server Dashboard", "3"); 
			
			mt.options.add(cpuItem); 
			mt.options.add(netItem); 
			mt.options.add(memItem); 
			mt.options.add(serverItem); 
	}

	selVal(getValue(baseUrl));
	selServerApplicationVal('s-'+getIdValue(baseUrl));
	
}
function initJvmMonitorType(baseUrl){
	//
	subBaseUrl = baseUrl;
	var mt= document.getElementById('monitor-type');

	if(mt){
		var heapItem = new Option("JVM Heap", "0"); 
		var noHeapItem = new Option("JVM No Heap", "1"); 
		var edenItem = new Option("JVM eden", "2"); 
		var oldEdenItem = new Option("JVM Old eden", "3"); 
		var surviorItem = new Option("JVM Survior", "4"); 
		var sessionItem = new Option("Tomcat Session", "5"); 
		var threadItem = new Option("JVM Thread", "6"); 
		var jvmItem = new Option("JVM Dashboard", "7"); 
		
		
		mt.options.add(heapItem); 
		mt.options.add(noHeapItem); 
		mt.options.add(edenItem); 
		mt.options.add(oldEdenItem); 
		mt.options.add(surviorItem); 
		mt.options.add(sessionItem); 
		mt.options.add(threadItem); 
		mt.options.add(jvmItem); 
	}

	selVal(getValue(baseUrl));
	selServerApplicationVal('a-'+getIdValue(baseUrl));
	
}

function initApplicationsType(key){
	var list = clustersMap[key];
	if(list && list.length >0){
		var mt= document.getElementById('applications');
		var allItem = new Option('All', '-1');
		mt.options.add(allItem);
		for(var i =0;i<list.length;i++){
			var appItem = new Option(list[i].name, list[i].id);
			mt.options.add(appItem);
		}
	}
}

function initBaseMonitorType(){
	//
	cleanMonitorType();
	var mt= document.getElementById('monitor-type');
	
	if(mt){
		var baseItem = new Option("No Selected", "0");
		mt.options.add(baseItem); 
	}
}

function selVal(val){
	var mt= document.getElementById('monitor-type');
	for(var i=0;i<mt.options.length;i++){
		if(mt.options[i].value==val){
			mt.options[i].selected=true;	
		}
	}
}
function selServerApplicationVal(val){
	var mt= document.getElementById('server-application');
	//option.attributes.exval.value
	for(var i=0;i<mt.options.length;i++){
		if(mt.options[i].value==val){
			mt.options[i].selected=true;
		}
	}
}

function onloadClean(){
	window.parent.initBaseMonitorType();
	window.parent.cleanApplications();
	window.parent.selServerApplicationVal(0);
}

function onloadServers(){
	window.parent.initServerMonitorType(document.baseURI);
}

function onloadApplications(){
	window.parent.initJvmMonitorType(document.baseURI);
}

function initDetailSelect(obj){
	var option = obj.options[obj.selectedIndex];
	if(option.attributes.type && option.attributes.type.value=='server'){
		cleanApplications();
		cleanMonitorType();
		initServerMonitorType(ctx+'/view/serverDetail?id='+option.attributes.exval.value+'&mt=3');
		doSelectChange(3);
	}else if(option.attributes.type && option.attributes.type.value=='application'){
		cleanMonitorType();
		initJvmMonitorType(ctx+'/view/appDetail?id='+option.attributes.exval.value+'&mt=7');
		doSelectChange(7);
	}else if(option.attributes.type && option.attributes.type.value=='cluster'){
		cleanApplications();
		initApplicationsType(option.text);
		var mt= document.getElementById('applications');
		onApplicationChange(mt);
	}

}
function onApplicationChange(obj){
	var option = obj.options[obj.selectedIndex];
	if(option.value=='-1'){
		var mt= document.getElementById('applications');
		var ids = '';
		for(var i=0;i<mt.options.length;i++){
			if(mt.options[i].value !='-1')
				ids +=mt.options[i].value+',';
		}
		cleanMonitorType();
		initJvmMonitorType(ctx+'/view/appDetail?id='+ids+'&mt=7');
		doSelectChange(7);
	}else{
		cleanMonitorType();
		initJvmMonitorType(ctx+'/view/appDetail?id='+option.value+'&mt=7');
		doSelectChange(7);
	}
}

function onSelect(obj){
	doSelectChange(obj.value);
}
function doSelectChange(value){

	if(subBaseUrl.match(/mt=\d+/g)){
		subBaseUrl = subBaseUrl.replace(/mt=\d/g,"mt="+value);
	}else if(subBaseUrl.indexOf('&')>0){
		subBaseUrl = subBaseUrl+'&mt='+value;
	}else{
		subBaseUrl = subBaseUrl+'?mt='+value;
	}
	//
	open(subBaseUrl,'main');
}

function getValue(url){
	var mt = url.match(/mt=\d+/g);
	if(mt != null){
		mt = mt + '';
		return mt.substring(3,mt.length)
	}
	return '0'
}
function getIdValue(url){
	var id = url.match(/id=\d+/g);
	if(id != null){
		id = id + '';
		return id.substring(3,id.length)
	}
	return '0'
}

function loadLog(id){
	setInterval(function () {
		if(loadstatus){
			$.getJSON(ctx+'/view/appLogDetail?id='+id,function (data){
				var obj = $("#applog");												
				for(var i=0;i<data.length;i++){
					obj.val(obj.val()+data[i]);
				}
			});
		}
	}, 2000);	
}

function stopLog(id){
	$.getJSON(ctx+'/view/appStopLog?id='+id,function (data){
		loadstatus = false;
		$('#lb-status').attr('class','label label-danger');
		$('#lb-status').text('Stop');
	});
}
function doStart(id){
	$.getJSON(ctx+'/view/appStart?id='+id,function (data){
		document.location.href=ctx+'/view/appLog?id='+id
	});
}
function doStop(id){
	$.getJSON(ctx+'/view/appStop?id='+id,function (data){
		document.location.href=ctx+'/view/appLog?id='+id
	});
}
function doGoBack(){
	document.location.href=ctx+'/view/applications'
}

function _alert (msg,color,id){
	if (id){
		var box = $(id).find('.alertBox');
		if (box.length == 0){
			box = $('<div class="alert-box"></div>');
			$(id).append(box);
		}
	}else{
		var box = $('#alertBox');
		if ( box.length == 0 ) {
			box = $('<div id="alertBox"></div>');
			$(document.body).append(box);
		}
	}


	var color = color || 'danger';
	var msg = $('<div class="bg-'+color+' alert-msg">'+msg+'</div>');
	box.append(msg);
	setTimeout(function(){
		msg.fadeOut('normal',function(){
			$(this).remove();
		});
	},3000)
}